<?php

namespace Home\Controller;

class PwdController extends CommonController {

    public function find() {
        $this->assign("ctitle", "找回密码");
        $this->display();
    }

    public function findPwd() {
        $config = getTableConfig();
        $data['account'] = I("post.email");
        $data['mtype'] = 1;
        $info = M("user")->field("name")->where("email = '" . $data['account'] . "'")->find();
        if (empty($info)) {
            echo -1;
            exit;
        }
        $name = $info['name'];
        $alphas = array("a", "b", "c", "d", "e", "f", "g");
        $data['rand'] = md5(array_rand($alphas) . rand(1, 9999999));

        $setPwdUrl = U("Pwd/set", array("email" => $data['account'], "rand" => $data['rand']));
        $subject = "找回密码[" . $config['title'] . __APP__ . "]";
        $body = "亲爱的" . $config['title'] . "会员，您好！

<div style='color:#FF8E33'><p>----------------------------------找回密码[" . $config['title'] . "<a href='" . __APP__ . "' target='_blank'>" . __APP__ . "</a>]-----------------------------------------<p>

<p style='text-indent:32px;'>亲爱的" . $name . ":</p>
<p>您好！感谢您使用" . $config['title'] . "订餐。</p>
<p>您提交了遗忘密码的申请，您可以通过点击更改密码进行密码修改。</p>
<p>如有任何疑问，请访问<a href='" . __APP__ . "' target='_blank'>" . $config['title'] . "</a>与我们取得联系。</p>

<p style='text-indent:32px;'>请单击或登陆后复制此链接进行操作<a href=" . $setPwdUrl . ">" . $setPwdUrl . "</p></div>

<p>------------------------------------------感谢您对" . $config['title'] . "的支持!----------------------------------------------</p>";
        sendMail($data['account'], $name, $subject, $body);
        M('codes')->add($data);
    }

    public function set() {
        $email = I('get.email'); //通过地址栏获取email
        $rand = I('get.rand'); //通过地址栏获取随机数
        $info = M("codes")->where("account='" . $email . "' AND rand = '" . $rand . "'")->find();

        $this->assign("email", $email);
        $this->assign("rand", $rand);
        $this->assign("ctitle", "设置密码");
        if (empty($info)) {
            $this->error('您的连接有误 请重新发送邮件！', U("Pwd/find"));
        } else {
            $this->display();
        }
    }

    public function set_post() {
        $data['pwd'] = md5(I('post.pwd')); //用户传过来的pwd
        $email = I('post.email'); //用户传过来的邮箱
        $rand = I('post.rand'); //用户的随机数
        $info = M("codes")->field("id")->where("account='" . $email . "' AND rand = '" . $rand . "' AND mtype = 1")->find();
        //  echo $Dao->getLastSql();
        if (!empty($info)) {
            M("codes")->where("account='" . $email . "'  AND mtype = 1")->delete();
            M("user")->where("account ='" . $email . "'")->save($data);
        } else {
            echo -1;
        }
    }

}

?>